package com.cashmate.struts;

import com.cashmate.hibernate.dao.BranchDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class EditBranch extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditBranch.class);
    private String oper;
    private String id;
    private String branchcode;
    private String branchname;
    private String location;
    private String branchmanager;
    private int bankaccountnum;
    private final BranchDAO branches = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private final EmployeeDAO empDao = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/edit-branch", results = {
        @Result(name = "duplicate", location = "../error/formError.jsp"),
        @Result(name = "error", location = "../error/messages.jsp")
    })
    @Override
    public String execute() {
        try {
            Branches b = null;
            if (oper.equals("add")) {
                if (branches.hasDuplicate(branchcode)) {
                    addActionError("ERROR : Branch code" + branchcode + " already exists.");
                    return "duplicate";
                }
                b = new Branches(branchname, location, "newbranch", branchcode, true, bankaccountnum);
                b.setEmployees(empDao.get(Integer.parseInt(branchmanager)));
                branches.insert(b);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") added a new Branch: {branchid:" + id + ", branchname:" + branchname + ", branchcode:" + branchcode + ", location:" + location + ", branchmanager:" + b.getBranchmanager() + ", bankaccountnum" + String.valueOf(b.getBankaccountnum()) + "}");
            } else if (oper.equals("edit")) {
                b = branches.get(Integer.parseInt(id));
                if (!b.getBranchcode().equals(branchcode)) {
                    if (branches.hasDuplicate(branchcode)) {
                        addActionError("ERROR : Branch code" + branchcode + " already exists.");
                        return "duplicate";
                    }
                }
                String old = b.getBranchcode();
                b.setBranchcode(branchcode);
                b.setLocation(location);
                b.setBranchname(branchname);
                b.setEmployees(empDao.get(Integer.parseInt(branchmanager)));
                b.setBankaccountnum(bankaccountnum);
                branches.update(b);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Branch(" + old + ") with new values: {branchid:" + id + ", branchname:" + branchname + ", branchcode:" + branchcode + ", location:" + location + ", branchmanager:" + b.getBranchmanager() + ", bankaccountnum" + String.valueOf(b.getBankaccountnum()) + "}");
            } else if (oper.equals("del")) {
                b = branches.get(Integer.parseInt(id));
                b.setIsactive(false);
                branches.update(b);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") deleted Branch(" + b.getBranchcode() + ")");
            }
        } catch (Exception e) {
            e.printStackTrace();;
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }

    public void setBranchcode(String branchcode) {
        this.branchcode = branchcode;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setBranchmanager(String branchmanager) {
        this.branchmanager = branchmanager;
    }

    public void setBranchname(String branchname) {
        this.branchname = branchname;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void setBankaccountnum(int bankaccountnum) {
        this.bankaccountnum = bankaccountnum;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }
}
